//
//  HCHeckLList.m
//  Depressed.domo
//
//  Created by rimi on 16/6/1.
//  Copyright © 2016年 薛涛. All rights reserved.
//

#import "HCHeckLList.h"

#define HMIN_HEIGHT 30
#define HMIN_WIDTH 80

@interface HCHeckLList ()
{
    CGFloat hHeight;
    CGFloat hWidth;
    BOOL isExpand;
    BOOL isAnimating;
}
@end

@implementation HCHeckLList

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"NoticeClose" object:nil];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _currentIndex = 0;
        isExpand = false;
        isAnimating = false;
        CGRect tmpFrame = frame;
        hWidth = frame.size.width;
        hHeight = frame.size.height;
        hWidth = hWidth < HMIN_WIDTH ? HMIN_WIDTH : hWidth;
        hHeight = hHeight < HMIN_HEIGHT ? HMIN_HEIGHT : hHeight;
        tmpFrame.size.width = hWidth;
        tmpFrame.size.height = hHeight;
        [self setFrame:tmpFrame];
        
        lbSelectText = [[UILabel alloc] initWithFrame:CRM(0, 0, hWidth - hHeight, hHeight)];
        [lbSelectText setBackgroundColor:[UIColor lightGrayColor]];
        [lbSelectText setText:@"Man"];
        
        btnExpand = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnExpand setBackgroundColor:[UIColor cyanColor]];
        [btnExpand setFrame:CRM(hWidth - hHeight, 0, hHeight, hHeight)];
        [btnExpand addTarget:self action:@selector(expandAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:lbSelectText];
        [self addSubview:btnExpand];
        
        tblSeleceList = [[UITableView alloc] initWithFrame:CRM(0, hHeight, hWidth, 100)];
        tblSeleceList.delegate = self;
        tblSeleceList.dataSource = self;
        [self addSubview:tblSeleceList];
        [self setClipsToBounds:true];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeTableList) name:@"NoticeClose" object:nil];
    }
    return self;
}

- (void)expandAction {
    if (isAnimating) {
        return;
    }
    isAnimating = true;
    
    if (!isExpand) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"NoticeClose" object:nil];
        [UIView animateWithDuration:0.35 animations:^{
            [self setFrame:CRM(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, hHeight + 100)];
        }completion:^(BOOL finished) {
            isExpand = true;
            isAnimating = false;
        }];
    }else {
       [UIView animateWithDuration:0.35 animations:^{
           [self setFrame:CRM(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, hHeight)];
       } completion:^(BOOL finished) {
           isExpand = false;
           isAnimating = false;
       }];
    }
}

- (void)closeTableList {
    if (isExpand) {
        [UIView animateWithDuration:0.35 animations:^{
            [self setFrame:CRM(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, hHeight)];
        } completion:^(BOOL finished) {
            isExpand = false;
            isAnimating = false;
        }];
    }
}

#pragma tabview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      static NSString *cellID = @"cellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text = [_arrDataList objectAtIndex:indexPath.row];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tblSeleceList deselectRowAtIndexPath:indexPath animated:true];
    _currentIndex = indexPath.row;
    [lbSelectText setText:[_arrDataList objectAtIndex:indexPath.row]];
    if ([_delegate respondsToSelector:@selector(checkListSelectAtIndex:text:checkList:)]) {
        [_delegate checkListSelectAtIndex:indexPath.row text:[_arrDataList objectAtIndex:indexPath.row] checkList:self];
    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_arrDataList && [_arrDataList count]) {
        return [_arrDataList count];
    }
    return 0;
}

- (void)setCurrentIndex:(NSInteger)currentIndex {
    _currentIndex = currentIndex;
    if (_arrDataList && [_arrDataList count]) {
        _currentIndex = _currentIndex >= _arrDataList.count ? _arrDataList.count - 1 : _currentIndex;
        [lbSelectText setText:[_arrDataList objectAtIndex:_currentIndex]];
    }
}

- (void)setArrDataList:(NSArray *)arrDataList {
    if (_arrDataList != arrDataList) {
        _arrDataList = arrDataList;
    }
    if (_arrDataList && [_arrDataList count]) {
        _currentIndex = _currentIndex >= _arrDataList.count ? _arrDataList.count - 1 : _currentIndex;
        [lbSelectText setText:[_arrDataList objectAtIndex:_currentIndex]];
    }
    [tblSeleceList reloadData];
}
@end
